﻿/*
 * Copyright (c) 2012 Loren M Halvorson
 * This source is subject to the Microsoft Public License (Ms-PL).
 * See http://www.microsoft.com/resources/sharedsource/licensingbasics/publiclicense.mspx.
 * All other rights reserved.
 * Portions copyright 2002-2007 The Genghis Group (http://www.genghisgroup.com/)
 */

using System;
using CookComputing.XmlRpc;

namespace ScrapeDocs
{
    /// <summary>
    /// Scrape html documentation from codeplex
    /// </summary>
    class Program
    {
        /// <summary>
        /// Mains entry point for ScrapeDocs
        /// </summary>
        /// <param name="args">The args.</param>
        /// <returns>0 if successful, nonzero if error occurred</returns>
        static int Main(string[] args)
        {
            int exitCode = 0;

            CommandLine cl = new CommandLine();
            if (!cl.ParseAndContinue(args))
                return 1;

            string password = cl.password;
            if (string.IsNullOrEmpty(password))
            {
                Console.Write("Password: ");
                password = ReadPassword();
            }

            BlogProxy scraper = new BlogProxy(cl.url);
            HtmlPageWriter pageWriter = new HtmlPageWriter();

            Console.WriteLine("Fetching posts from {0}...", scraper.Url);
            XmlRpcStruct[] posts = scraper.GetRecentPosts(cl.blog, cl.user, password, cl.posts);

            foreach (XmlRpcStruct post in posts)
            {
                string title = (string)post["title"];
                string body = (string)post["description"];

                Console.WriteLine(" Writing {0}.", title);

                pageWriter.WriteHtml(title, body, cl.template, cl.output);
            }

            Console.WriteLine("Complete.");

            return exitCode;
        }


        /// <summary>
        /// Reads the password.
        /// </summary>
        /// <returns></returns>
        public static string ReadPassword()
        {
            string password = "";
            ConsoleKeyInfo info = Console.ReadKey(true);
            while (info.Key != ConsoleKey.Enter)
            {
                if (info.Key != ConsoleKey.Backspace)
                {
                    Console.Write("*");
                    password += info.KeyChar;
                }
                else if (info.Key == ConsoleKey.Backspace)
                {
                    if (!string.IsNullOrEmpty(password))
                    {
                        // remove one character from the list of password characters
                        password = password.Substring(0, password.Length - 1);
                        // get the location of the cursor
                        int pos = Console.CursorLeft;
                        // move the cursor to the left by one character
                        Console.SetCursorPosition(pos - 1, Console.CursorTop);
                        // replace it with space
                        Console.Write(" ");
                        // move the cursor to the left by one character again
                        Console.SetCursorPosition(pos - 1, Console.CursorTop);
                    }
                }
                info = Console.ReadKey(true);
            }
            // add a new line because user pressed enter at the end of their password
            Console.WriteLine();
            return password;
        }
    }
}
